﻿using System;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.dz.httpmoudle;
using com.dz.common;
using System.Text;
namespace com.dz.web
{
    public class BasePage : Page
    {
        protected string c_title_name = ",手打,文字,txt,小说阅读_体验码小说网|免费小说,玄幻小说,武侠小说,言情小说,小说网各类小说在线阅读,下载";
        protected override void OnLoad(EventArgs e)
        {
            if (DateTime.Now > new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 4, 0, 0) && DateTime.Now < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 7, 0, 0))
            {
                LogHelper.Info(SystemUtil.UserTrueIp);
            }
            if (Request.Url.ToString().ToLower().Contains("/admin"))
            {
                string userName = CookieManager.GetLoginUser();
                if (string.IsNullOrEmpty(userName))
                {

                    Response.Redirect("/admin/Login.aspx", true);

                }
              
            }
            base.OnLoad(e);
        }
        /// <summary>
        /// 用户登录Token
        /// </summary>
        protected UserToken Token
        {
            get
            {
                UserToken token = HttpContext.Current.Items[AuthenticateModule.CONTEXT_ROLE_KEY] as UserToken;
                return token;
            }

        }

        
        /// <summary>
        /// 获取频道id和名称
        /// </summary>
        /// <param name="i_channel_name"></param>
        /// <param name="o_channel_china_name"></param>
        /// <returns></returns>
        protected string GetCategoryIdByChannel(string i_channel_name,out string o_channel_china_name,out string o_channel_id)
        {
            o_channel_china_name = "";
            o_channel_id="1";

            string t_wuxia_xianxia_id = "27,42,40,3,9,34,10";
            string t_xuanhua_qihuan_id = "43,20,25,62,38,21,48,16,56";
            string t_dushi_yanqing_id = "46,53,49,12,28,22,17,6,52,11,51,30";
            string t_lishi_junshi_id = "4,59,1,55,32,45,24,5,26,47,36,19";
            string t_youxi_jingji_id = "58,8,60,65,37,23,15";
            string t_kehuan_yiliang_id = "6,41,39,14,50,44,18,54,64,33,2,29";

            string t_meiren_tongwen_id = "109";
            string t_nvpin_danmei_id = "64";
            string t_chuban_book_id = "65";

            string t_lishi_id = "4,59,1,55,32,45,24,5,26,47";
            string t_xiandai_id = "46,53,49,12,28,22,17,6,52,11";
            string t_kehuan_id = "50,44,18,54,64,33,2,29";
            string t_xuanqing_id = "17,6,52,11,51,30";

            string t_categoryId_list = "";
            switch (i_channel_name.ToLower())
            {
                case "wuxiaxianxia":
                    o_channel_china_name="武侠仙侠";
                    t_categoryId_list = t_wuxia_xianxia_id;
                    o_channel_id = "2";
                    break;
                case "xuanhuanqihuan":
                    o_channel_china_name="玄幻奇幻";
                    t_categoryId_list = t_xuanhua_qihuan_id;
                    o_channel_id = "1";
                    break;
                case "dushiyanqing":
                    o_channel_china_name="都市言情";
                    t_categoryId_list = t_dushi_yanqing_id;
                    o_channel_id = "3";
                    break;
                case "lishijunshi":
                    o_channel_china_name="历史军事";
                    t_categoryId_list = t_lishi_junshi_id;
                    o_channel_id = "4";
                    break;
                case "youxijingji":
                    o_channel_china_name="游戏竞技";
                    t_categoryId_list = t_youxi_jingji_id;
                    o_channel_id = "5";
                    break;
                case "kehuanyiling":
                    o_channel_china_name="科幻异灵";
                    t_categoryId_list = t_kehuan_yiliang_id;
                    o_channel_id = "6";
                    break;
                case "tongrenmeiwen":
                    o_channel_china_name="同人美文";
                    t_categoryId_list = t_meiren_tongwen_id;
                    o_channel_id = "7";
                    break;
                case "nvpindanmei":
                    o_channel_china_name = "女频耽美";
                    t_categoryId_list = t_nvpin_danmei_id;
                    o_channel_id = "8";
                    break;
                case "publishbook":
                    o_channel_china_name = "出版书籍";
                    t_categoryId_list = t_chuban_book_id;
                    o_channel_id = "9";
                    break;
                case "lishi":
                    o_channel_china_name = "历史馆";
                    t_categoryId_list = t_lishi_id;
                    break;
                case "xiandai":
                    o_channel_china_name = "现代馆";
                    t_categoryId_list = t_xiandai_id;
                    break;
                case "kehuan":
                    o_channel_china_name = "科幻馆";
                    t_categoryId_list = t_kehuan_id;
                    break;
                case "xuanqing":
                    o_channel_china_name = "玄情馆";
                    t_categoryId_list = t_xuanqing_id;
                    break;
                default:
                    t_categoryId_list="";
                    break;
            }
            if (t_wuxia_xianxia_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "2";
            }
            else if (t_xuanhua_qihuan_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "1";
            }
            else if (t_dushi_yanqing_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "3";
            }
            else if (t_lishi_junshi_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "4";
            }
            else if (t_youxi_jingji_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "5";
            }
            else if (t_kehuan_yiliang_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "6";
            }
            else if (t_meiren_tongwen_id.IndexOf(i_channel_name) > -1)
            {
                o_channel_id = "7";
            }
            string t_reg = "\\d+";
            if (Regex.IsMatch(i_channel_name, t_reg))
                return i_channel_name;
            return t_categoryId_list;
        }

        /// <summary>
        /// 定向到错误页面
        /// </summary>
        protected void RedirectErrorPage()
        {
            Response.Redirect("/Error.aspx", true);
        }

        /// <summary>
        /// 检查访问权限
        /// </summary>
        /// <returns></returns>
        bool CheckPermission()
        {
            return false;

            //try
            //{
            //    string url = Request.Url.ToString().ToLower();
            //    url = url.Replace("/", "\\");
            //    int index = url.LastIndexOf("\\");
            //    string pageName = url.Substring(index + 1);
            //    SoucrceManager sourceMgr = new SoucrceManager();
            //    SourceInfo info = sourceMgr.GetSourceByName(pageName);

            //    if (info == null)
            //        return true;

            //    if (Token == null)
            //        return false;

            //    if (string.IsNullOrEmpty(Token.RoleString))
            //        return false;

            //    string[] arr = Token.RoleString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //    if (arr == null || arr.Length < 0)
            //        return false;
            //    bool permission = false;
            //    for (int i = 0; i < arr.Length; i++)
            //    {
            //        if (arr[i].ToString().Equals(info.id.ToString()))
            //        {
            //            permission = true;
            //            break;
            //        }
            //    }

            //    return permission;

            //}
            //catch (Exception ex)
            //{
            //    LogHelper.LogError("判断权限出错" + ex.ToString());
            //}
            //return false;

        }

        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();
            LogHelper.Error(ex.ToString());
            Response.Redirect("/Error.aspx", true);
        }

        protected void RedirectErrorPage(string msg)
        {
            Response.Redirect("/Error.aspx?msg="+HttpUtility.UrlEncode(msg), true);
        }

        protected void JSAlert(string alert)
        {
            const string strTemp = "<script language='javascript'> alert('{0}') </script>";
            string str = String.Format(strTemp, alert);

            Response.Write(str);
        }

        /// <summary>
        /// 获取分页HTML
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        protected string GetPageHtml(int pageIndex, int pageSize, int count, string pageUrl)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("<div id=\"pager_blockdd\">");
            sb.Append("<div id=\"pager\">");
            int pageCount = (int)Math.Ceiling((double)count / pageSize);
            int start = ((pageIndex - 1) / 10) * 10 + 1;
            for (int i = start; i <= pageCount && i < start + 10; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<span title=\"当前第{0}页\" class=\"current\">{1}</span>", i, i);
                else
                    sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页\">{2}</a>", pageUrl + i, i, i);
            }
            if (start > 1)
            {
                sb.Insert(0, String.Format("<a href=\"{0}\" title=\"第{1}页\">...</a>", pageUrl + (start - 1), (start - 1)));
                sb.Insert(0, String.Format("<a href=\"{0}\" title=\"首页\">{1}</a>", pageUrl + 1, 1));
            }
            if (start + 10 <= pageCount)
            {
                sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页\">...</a>", pageUrl + (start + 10), start + 10);
                sb.AppendFormat("<a href=\"{0}\" title=\"第{1}页(末页)\">{2}</a>", pageUrl + pageCount, pageCount, pageCount);
            }
            sb.Append("</div></div>");
            return sb.ToString();
        }


     
  

    }
}
